#include "IS.h"
#include <stdlib.h>
#include <time.h>
#include "RSManager.h"
#include "Box.h"
#include "QuadTree.h"
#include "ShareComponents.h"
#include <list>
using namespace std;
#include "QuadTree.h"
#include "StaticObject.h"
#include "GameDefine.h"
#include "Camera.h"
#include "MainGame.h"
IS::~IS(void)
{
}

IS::IS(Box* box): Object(box, ObjAnti_DYNAMID), MoveObject()
{		
	init();
}

void IS::init()
{

	RSManager* rs = RSManager::getInstance();
	this->left = rs->getSprite(IS_GO_LEFT);	
	this->right = rs->getSprite(IS_GO_RIGHT);
	this->jump_left = rs->getSprite(IS_JUMP_LEFT);
	this->jump_right = rs->getSprite(IS_JUMP_RIGHT);
	sink = rs->getSprite(SINK);
	_sprite = _box->_v.x > 0.0f?jump_right:jump_left;	
	timeDelay =0;	
	this->AssignMove(_box);		
	_box->_v.y = 0;	
	_box->_size = _sprite->getRenderSize();	
	_hits = 1;	
	is_state = IS_JUMP;
	delay = 0;
	float size_x = _box->_size.x / 2.0f;
	float size_y = _box->_size.y - 5;
	_colBox->_size.x = size_x;
	_colBox->_size.y = size_y;
}

void IS::update()
{	
	delay += GAME_TIME;
	if(delay > 100)
	{
		_sprite->Next();
		delay = 0;
	}
	move();

	switch (is_state)
	{
	case IS_JUMP:
		{
			if(_hits <= 0)
			{
				is_state = IS_INJURE;
				_box->_v.y = 5;
				_sprite = _box->_v.x > 0.0f ? jump_right:jump_left;
				_box->_v.x = 0;
				return;
			}
			_colBox->_position = _box->_position;
			_colBox->_v = _box->_v;			
			_colBox->_position.x += _colBox->_size.x / 2.0f;
			_colBox->_position.y += 5.0f;
			if(isOnGround() || isOnOthers())
			{
				_sprite = _box->_v.x > 0.0f ? right : left;
				is_state = IS_RUN;
				return;
			}
			if(isOnWater())
			{
				_sprite = sink;
				_box->_v.x = 0.0f;
				is_state = IS_SINK;
			}			
		}
		break;
	case IS_RUN:
		{
			if(_hits <= 0)
			{
				is_state = IS_INJURE;
				_box->_v.y = 5;
				_sprite = _box->_v.x > 0.0f ? jump_right:jump_left;
				_box->_v.x = 0;
				return;
			}
			_colBox->_position = _box->_position;
			_colBox->_v = _box->_v;
			_colBox->_v.y = 0;
			_colBox->_position.x += _colBox->_size.x / 2.0f;
			_colBox->_position.y += 5.0f;
			if(isOnSpace())
			{
				is_state = IS_JUMP;				
				return;
			}
			StaticObject* ground = _getCurrentGround();			
			bool right_ck = ground->getBox()->getRight() < _box->getRight();
			bool left_ck = ground->getBox()->getLeft() > _box->getLeft();
			if(isOnGround())
			{
				if(right_ck || left_ck)
				{
					int decision = rand() % 3;
					bool is_go_right = _box->_v.x > 0;
					switch (decision)
					{
					case 0:
					case 1:
						{
							if((is_go_right && right_ck)||(!is_go_right && left_ck))
							{
								_sprite = is_go_right ? jump_right:jump_left;
								_box->_v.y = rand() % 8;
								is_state = IS_JUMP;
							}
						}
						break;
					case 2:
						{
							_sprite = is_go_right ? left : right;
							_box->_v.x *= -1.0f;
						}
						break;
					}
				}
			}
		}
		break;
	case IS_INJURE:
		{
			if(_box->_v.y < 0.0f)
			{
				is_state = IS_DEATH;
			}
			_colBox->_position = gl_BOX_0_0_0->_position;
			_colBox->_v = gl_BOX_0_0_0->_v;
			_colBox->_size = gl_BOX_0_0_0->_size;
		}
		break;
	case IS_DEATH:
		{
			_isAlive = false;
		}
		break;
	case IS_SINK:
		{
			timeDelay += GAME_TIME;
			if(timeDelay > 80)
			{
				timeDelay = 0;
				is_state = IS_DEATH;
			}
			_colBox->_position = gl_BOX_0_0_0->_position;
			_colBox->_v = gl_BOX_0_0_0->_v;
			_colBox->_size = gl_BOX_0_0_0->_size;
		}
		break;
	}
}

void IS::render()
{
	this->_sprite->Render(_box->_position);
	Object::render();
}

Box* IS::getCollisionBox()
{	
	return _colBox;
}

